package com.ruoyi.material.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.material.domain.MateEvaluate;
import com.ruoyi.material.domain.MaterialManufactuter;
import com.ruoyi.material.mapper.MateManufactuterMapper;
import com.ruoyi.material.service.MateManufactuterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */

@RestController
@RequestMapping("/matemanufacturer")
public class MaterialManufactuterController extends BaseController {

    @Autowired
    MateManufactuterService mateManufactuterService;

    @Autowired
    MateManufactuterMapper mateManufactuterMapper;

    /**
     * 新增保存原材料生产商管理
     */
    @RequiresPermissions("business:manufacturer:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody MaterialManufactuter djManufacturer) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        djManufacturer.setCreatBy(sysUser.getUserName());
        djManufacturer.setCompanyId(sysUser.getCompanyId());
        return toAjax(mateManufactuterService.insertMateManufacturer(djManufacturer));
    }

    /**
     * 查询生产商管理列表
     */
    @RequiresPermissions("business:manufacturer:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(value = "materialName", required = false) String materialName) {
        startPage();
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        MaterialManufactuter materialManufactuter = new MaterialManufactuter();
        materialManufactuter.setMaterialName(materialName);
        materialManufactuter.setCompanyId(sysUser.getCompanyId());
        List<MaterialManufactuter> list = mateManufactuterService.selectMateManufactuter(materialManufactuter);
        return getDataTable(list);
    }

    @GetMapping("/selectInformation")
    public List<MaterialManufactuter> selectMaterialManufacturer(@RequestParam(value = "materialId", required = false) Long materialId) {
        MaterialManufactuter materialManufactuter = new MaterialManufactuter();
        materialManufactuter.setMaterialId(materialId);
        materialManufactuter.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
        return mateManufactuterMapper.selectMateManufactuterById(materialManufactuter);
    }

    /**
     * 修改保存生产商管理
     */
    @RequiresPermissions("business:manufacturer:add")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody MaterialManufactuter materialManufactuter) {
        return toAjax(mateManufactuterService.updateMateManufacturer(materialManufactuter));
    }

    @RequiresPermissions("business:manufacturer:add")
    @PostMapping("/addEvaluate")
    @ResponseBody
    //添加供方评价表
    public AjaxResult addEvaluate(@RequestBody MateEvaluate mateEvaluate) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        mateEvaluate.setDeptId(sysUser.getDeptId());
        int num = mateManufactuterService.insertEvaluate(mateEvaluate);
        if (num > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.success("添加失败");
        }
    }

    /*
    查询已经做完的供方评价表
     */
    @GetMapping("/selectEdit")
    public Map<String, Object> selectEdit(@RequestParam(value = "evaluateId", required = false) Long evaluateId) {
        Map<String, Object> resMap = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        resMap.put("evaluateId", evaluateId);
        MateEvaluate mateEvaluate = mateManufactuterService.selectEdit(resMap);
        result.put("mateEvaluate", mateEvaluate);
        return result;
    }


    /**
     * 修改保存供方评价表
     */
    @PostMapping("/updateAdvice")
    @ResponseBody
    public AjaxResult updateAdvice(@RequestBody MateEvaluate mateEvaluate) {
        int num = mateManufactuterService.updateAdvice(mateEvaluate);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    /*
      查询已经做完的供方评价表
       */
    @GetMapping("/selectEvaluateList")
    public TableDataInfo selectEvaluateList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                            @RequestParam(value = "endTime", required = false) String endTime) {
        startPage();
        MateEvaluate mateEvaluate=new MateEvaluate();
        mateEvaluate.setDeptId(SecurityUtils.getDeptId());
        mateEvaluate.setBeginTime(beginTime);
        mateEvaluate.setEndTime(endTime);
        List<MateEvaluate> mateEvaluates = mateManufactuterService.selectEvaluateList(mateEvaluate);
        return getDataTable(mateEvaluates);
    }


}
